package com.lg.system.role.controller;


import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.role.entity.Role;
import com.lg.system.role.service.RoleService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author DL
 * @since 2018-03-19
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController<Role, RoleService> {
	
	private String prefix="system/role/";
	@Autowired
	RoleService roleService;

	@RequiresPermissions("role:role")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("role:role")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody Role role){
		Query<Role> query = new Query<Role>(role);
		List<Role> roles=roleService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(roles,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("role:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@RequiresPermissions("role:edit")
	@GetMapping("/edit/{roleId}")
	public ModelAndView edit(@PathVariable("roleId") Long roleId){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", roleService.selectById(roleId));
		return mView;
	}
	
	@RequiresPermissions("role:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) Role role,BindingResult bindingResult){
		role.setCreateId(getUserId());
		if(roleService.save(role)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("role:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) Role role,BindingResult bindingResult){
		role.setModifiedId(getUserId());
		if(roleService.update(role)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("role:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) Role role,BindingResult bindingResult){
		if(roleService.deleteById(role.getRoleId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	
	/** 
	* @Description: 添加用户时需要选择用户角色此处查询所有角色
	* @param @return  
	* @return List<Role> 
	* @throws 
	*/ 
	@RequestMapping("/selectAllRoles")
	public List<Role> selectAllRoles(){
		EntityWrapper<Role>  wrapper = new EntityWrapper<Role>();
		if(!getUserId().equals(1L)){
			wrapper.notIn("role_sign", "administrator");//非超级管理员添加用户选择角色时只能添加超级管理员以外的角色
		}
		List<Role> roles=roleService.selectList(wrapper);
		return roles;
	}

}
